package com.note.services.listener;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.note.common.constants.RabbitConst;
import com.note.common.domain.Log;
import com.note.services.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Package：com.note.services.mq
 * @Name：LogMqListener
 * @Author：热伊木
 * @Email：uyevan@163.com
 * @Date：2024-05-01-11:21
 * @Description：Mq消息监听者 --- 监听日志
 */

@Slf4j
@Component
public class LogMqListener {

    @Autowired
    private LogService logService;

    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(RabbitConst.RabbitMQTopic))
    public void Message(String message) {
        if (StringUtils.isNotBlank(message)) {
            try {
                //TODO 日志转对象
                Log dLog = JSON.parseObject(message, Log.class);
                log.info("message:{}", dLog);
                //TODO 存储日志
                logService.inputLog(dLog);
            } catch (Exception e) {
                log.error("BITCH!{}", e);
            }
        }
    }

}
